package com.joshua.ticket.manager.controllers;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.joshua.userservice.models.Continent;

@Controller
@RequestMapping("/continent")
public class ContinentController {

	@Autowired
	public SessionFactory sessionFactory;

	@RequestMapping("index")
	public ModelAndView displaySortedMembers() {
		/*
		 * String name ="joshua"; Date date = new Date();
		 * 
		 * 
		 * mav.addObject("name",name); mav.addObject("date", date);
		 */
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		List objs = session.createQuery(
				"from" + Continent.class.toString().replace("class", ""))
				.list();

		List<Continent> continents = (List<Continent>) objs;
		ModelAndView mav = new ModelAndView("continent/index");

		tx.commit();
		session.close();
		mav.addObject("continents", continents);
		return mav;
	}

	@RequestMapping("edit")
	public ModelAndView edit(String id) {
		ModelAndView mav = new ModelAndView("continent/edit");
		try {
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			Continent continent = (Continent) session.get(Continent.class,
					Long.parseLong(id));
			tx.commit();
			session.close();
			mav.addObject("continent", continent);
		} catch (Exception ex) {
			return displaySortedMembers();
		}
		return mav;
	}

	@RequestMapping("update")
	public ModelAndView update(Continent continent) {
		try {
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			session.merge(continent);
			tx.commit();
			session.close();

		} catch (Exception ex) {
			ModelAndView mav = new ModelAndView("redirect:edit.do");
			return mav;
		}
		ModelAndView mav = new ModelAndView("redirect:index.do");
		return mav;
	}

	@RequestMapping("createtab")
	public ModelAndView createtab() {
		ModelAndView mav = new ModelAndView("continent/create");
		return mav;
	}

	@RequestMapping("create")
	public ModelAndView create(Continent continent) {
		try {
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			session.save(continent);
			tx.commit();
			session.close();
		} catch (Exception ex) {
			ModelAndView mav = new ModelAndView("redirect:create.do");
			return mav;
		}
		ModelAndView mav = new ModelAndView("redirect:index.do");
		return mav;
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public @ResponseBody
	JsonResponse delete(@RequestBody Continent continent) {
		JsonResponse jsonResponse = new JsonResponse();
		// String id = "999";
		try {
			// Continent continent = new Continent();
			// continent.setId(Long.parseLong(id));
			Session session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			session.delete(continent);
			tx.commit();
			session.close();

			jsonResponse.setException("None");
			jsonResponse.setResultState("Success");
			jsonResponse.setResult("Delete Continent with id:"
					+ continent.getId());
		} catch (Exception ex) {
			jsonResponse.setException(ex.toString());
			jsonResponse.setResultState("Failure");
			jsonResponse.setResult("Delete Continent with id:"
					+ continent.getId() + "Failure");
		}

		return jsonResponse;
	}

}
